#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"{
#endif
#define USE_CONSOLE  1
#if USE_CONSOLE
#define USE_CLOSE_CONSLE 0
BOOL WINAPI CtrlHandler(DWORD CtrlType)
{
	if(CTRL_CLOSE_EVENT==CtrlType)
	{
		FreeConsole();
	}

	return TRUE;

}	
		
void OpenConsoleWindow()
{

	AllocConsole();
	//SetConsoleCtrlHandler(CtrlHandler,TRUE);

	int hCrt = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE),_O_TEXT);
	FILE * hf = _fdopen( hCrt, "w" );
	*stdout = *hf;
	setvbuf( stdout, NULL, _IONBF, 0 );

	// Console Window ...
	printf("Debug Message ... \n\n");

}

void CloseConsoleWindow()
{

	FreeConsole();

}

void ShowConsoleWindow()
{
	OpenConsoleWindow();
}

void HideConsoleWindow()
{

	HWND hwnd = ::GetConsoleWindow();
	if (hwnd != NULL)
	{
		CloseConsoleWindow();
	}

}

#else
	BOOL WINAPI CtrlHandler(DWORD CtrlType)
	{
		return TRUE;
	}	

	void OpenConsoleWindow()
	{
	}

	void CloseConsoleWindow()
	{
	}

	void ShowConsoleWindow()
	{
	}

	void HideConsoleWindow()
	{
	}
#endif

#ifdef __cplusplus
}
#endif